class Entry < ActiveRecord::Base

  belongs_to :user
  belongs_to :spot
  
  def spot_name
    spot ? spot.name : nil
  end
  
  def lat
    spot ? spot.latitude : nil
  end
  
  def long
    spot ? spot.longitude : nil
  end

  def self.post_entry(attributes)

    logger.debug "post_entry:#{attributes.inspect}"

    spot = Spot.find(:first, :conditions => "(latitude = '#{attributes[:lat]}') and (longitude = '#{attributes[:long]}')")
    if spot.nil?
      spot_attrs = {
        :name => attributes[:spot_name],
        :latitude => attributes[:lat],
        :longitude => attributes[:long]
      }
      spot = Spot.create!(spot_attrs)
    end
    [:spot_name, :lat, :long].each{|attr|attributes.delete(attr)}
    Entry.create!(attributes.merge(:spot_id => spot.id))
  end
end
